package com.tmall.tmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tmall.tmall.mapper.*;
import com.tmall.tmall.pojo.*;
import com.tmall.tmall.service.OrderService;
import com.tmall.tmall.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderitemMapper orderitemMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImageMapper productImageMapper;
    @Autowired
    private ShoppingcartMapper shoppingcartMapper;

    @Override
    @Transactional
    public void insert(Orders orders) {
        //插入订单
        orders.setOrderCode(UUID.randomUUID().toString());
        orders.setCreateDate(LocalDateTime.now());
        orders.setStatus(1);
        List<Products> productList = orders.getProductList();
        BigDecimal sum = new BigDecimal(0);
        Integer total=0;
        for (Products products : productList) {
            BigDecimal price = productMapper.getById(products.getPid()).getPrice();
            sum.add(price.multiply(BigDecimal.valueOf(products.getNumber())));
            products.setSum(price.multiply(BigDecimal.valueOf(products.getNumber())));
            total+=products.getNumber();
        }
        orders.setSum(sum.doubleValue());
        orders.setTotalNumber(total);
        orders.setUid(ThreadLocalUtils.getUid());
        if(orders.getPost()==null)orders.setPost("无");
        if(orders.getUserMessage()==null)orders.setUserMessage("无");
        orderMapper.insert(orders);
        //订单详情数据
        Integer uid = ThreadLocalUtils.getUid();
        for (Products product : productList) {
            shoppingcartMapper.deleteByUidAndPid(uid,product.getPid());
            product.setOid(orders.getId());
            product.setStatus(1);
            product.setCreateDate(LocalDateTime.now());
            productMapper.updateSS(product.getPid(),product.getNumber());
        }
        orderitemMapper.insertBatch(productList);
    }

    @Override
    @Transactional
    public void update(Orders orders) {
        orderMapper.update(orders);
    }

    @Override
    public Orders getById(Orders orders) {
        return orderMapper.getById(orders.getId());
    }

    @Override
    public PageRt page(Integer pageNum, Integer pageSize, String username, String name, String tel, String address, Integer status,Integer uid) {
        PageHelper.startPage(pageNum, pageSize);
        //先获取所有符合要求的订单
        List<OrdersRt> ordersList = orderMapper.page(name,tel,address,status,uid,username);

        for (OrdersRt ordersRt : ordersList) {
            //通过订单中的商品id在详情中找到商品
            Integer oid = ordersRt.getOid();
            List<ProductRt>productRtList = orderitemMapper.selectByOid(oid);
            for (ProductRt productRt : productRtList) {
                String mainImg = productImageMapper.getMainByPid(productRt.getId());
                String productName=productMapper.getNameById(productRt.getId());
                productRt.setMainImg(mainImg);
                productRt.setName(productName);
            }
            ordersRt.setProductRts(productRtList);
        }
        PageInfo<OrdersRt> pageInfo = new PageInfo<>(ordersList);
        return new PageRt(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
            orderitemMapper.deleteByOid(id,LocalDateTime.now());
            Orders orders = Orders.builder().id(id).deleteAt(LocalDateTime.now()).build();
            orderMapper.update(orders);
        }
    }

    @Override
    @Transactional
    public void pay(Integer oid) {
        //修改状态
        Orders orders = Orders.builder().id(oid).status(2).payDate(LocalDateTime.now()).build();
        orderMapper.update(orders);
        //修改明细状态
        orderitemMapper.uStatus(oid,2);
    }

    @Override
    public OrdersRt getOrderDetail(Integer oid) {
        Orders orderInfo = orderMapper.getById(oid);
        
        if (orderInfo == null) {
            return null;
        }

        OrdersRt orderDetail = new OrdersRt();
        orderDetail.setOid(orderInfo.getId());
        orderDetail.setUid(orderInfo.getUid());
        orderDetail.setSum(String.valueOf(orderInfo.getSum()));
        orderDetail.setNumber(String.valueOf(orderInfo.getTotalNumber()));
        orderDetail.setStatus(orderInfo.getStatus());
        orderDetail.setName(orderInfo.getName());
        orderDetail.setTel(orderInfo.getTel());
        orderDetail.setAddress(orderInfo.getAddress());
        orderDetail.setUserMessage(orderInfo.getUserMessage());
        orderDetail.setCreateDate(orderInfo.getCreateDate());

        List<ProductRt> productRtList = orderitemMapper.selectByOid(oid);
        for (ProductRt productRt : productRtList) {
            String mainImg = productImageMapper.getMainByPid(productRt.getId());
            String productName = productMapper.getNameById(productRt.getId());
            productRt.setMainImg(mainImg);
            productRt.setName(productName);

            if (productRt.getPrice() == null) {
                BigDecimal price = productMapper.getPriceById(productRt.getId());
                productRt.setPrice(price);
            }
        }
        orderDetail.setProductRts(productRtList);
        
        return orderDetail;
    }

    @Override
    public void delivery(Integer oid, Integer pid) {
        //修改明细表中的状态
        //每个商品独立发货
        //变成待收货
        orderitemMapper.deliver(oid,pid,3,LocalDateTime.now());
    }

    @Override
    public void confirm(Integer oid, Integer pid) {
        //收获
        //变成待评论
        orderitemMapper.confirm(oid,pid,4,LocalDateTime.now());
    }

    @Override
    public void cancel(Integer oid) {
        //订单和商品一起取消
        Orders orders = Orders.builder().id(oid).status(6).payDate(LocalDateTime.now()).build();
        orderMapper.update(orders);
        orderitemMapper.uStatus(oid,6);
        //把数量加回去
        List<Orderitem> list = orderitemMapper.getByOid(oid);
        for (Orderitem orderitem : list) {
            productMapper.updateSS(orderitem.getPid(),-1*orderitem.getNumber());
        }
    }
}
